package com.example.administrator.mydemo;

import android.content.Context;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Create by jnn on 2018/5/16
 */
public class MyProgressView extends RelativeLayout {
    private Context context;
    private TextView[] mTxtTabs;
    private ProgressBar progressBar;
    private ImageView ivThumb;

    private int max = 30;//默认100
    private int range;//可滑动的范围
    private float proMargin = 20;//进度条左间距
    private float proHeight = 10;//进度条高度
    private LayoutParams ivParam; //滑块图片 参数
    private float imgSize = 45; //滑块图片大小
    private float bgTopMargin = -20;//背景to滑块的上间距
    private float bgHeight = 25;//背景的高度

    public MyProgressView(Context context) {
        this(context, null);
    }

    public MyProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {
        /**背景**/
        RelativeLayout rlBgRoot = new RelativeLayout(context);
        rlBgRoot.setBackground(getResources().getDrawable(R.drawable.bg));
        LayoutParams rlBgParam = new LayoutParams(LayoutParams.MATCH_PARENT, dip2px(bgHeight));
        //滑块 和 背景的位置关系,相对于图片，位于图片下方，但是图片位置希望与进度条靠近，于是设置为负数
        rlBgParam.addRule(RelativeLayout.BELOW, R.id.ivThumb);
        rlBgParam.topMargin = dip2px(bgTopMargin);
        rlBgRoot.setLayoutParams(rlBgParam);
        rlBgRoot.setId(R.id.rlRoot);

        /**进度**/
        progressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
        progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.seekbar_define_style));
        LayoutParams proParams = new LayoutParams(LayoutParams.MATCH_PARENT, dip2px(proHeight));
        proParams.leftMargin = dip2px(proMargin);
        proParams.rightMargin = dip2px(proMargin);
        proParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);//addRule参数对应RelativeLayout XML布局的属性
        progressBar.setLayoutParams(proParams);
        progressBar.setId(R.id.progress);

        /**添加背景容器**/
        rlBgRoot.addView(progressBar);
        addView(rlBgRoot, 0);

        /**添加图片view**/
        ivThumb = new ImageView(context);
        ivThumb.setImageResource(R.mipmap.rabit);
        ivThumb.setId(R.id.ivThumb);
        ivParam = new LayoutParams(dip2px(imgSize), dip2px(imgSize));
        ivThumb.setLayoutParams(ivParam);
        addView(ivThumb, 1);

        //添加文字
        View bottomFont = LayoutInflater.from(context).inflate(R.layout.bottom_font, null);
        LinearLayout rootLayout = (LinearLayout) bottomFont.findViewById(R.id.ll);
        LayoutParams bottomparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        bottomparams.addRule(RelativeLayout.BELOW, R.id.rlRoot);
        bottomparams.topMargin = dip2px(10);
        bottomparams.leftMargin = dip2px(10);
        bottomparams.rightMargin = dip2px(10);
        bottomFont.setLayoutParams(bottomparams);
        addView(bottomFont, 2);

        //底部字体
        mTxtTabs = new TextView[5];
        mTxtTabs[0] = (TextView) rootLayout.getChildAt(0);
        mTxtTabs[1] = (TextView) rootLayout.getChildAt(1);
        mTxtTabs[2] = (TextView) rootLayout.getChildAt(2);
        mTxtTabs[3] = (TextView) rootLayout.getChildAt(3);
        mTxtTabs[4] = (TextView) rootLayout.getChildAt(4);
    }

    /**
     * 设置进度
     *
     * @param progress
     */
    public void setProgress(final int progress) {
        new android.os.Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                range = progressBar.getWidth();
                if (progress >= 0 && progress <= max) {
                    //计算进度对应的位置
                    if (range != 0) {
                        float fen = range / (max * 1.0f);//宽/30
                        int left = (int) (progress * fen);
                        Log.i("tag", "left:" + left + "  fen:" + fen + "  range:" + range);
                        setThumbPosition(left);
                        setProgressBarProgress(progress);
                    }
                } else {
                    if (progress > max) {
                        setThumbPosition(range);
                        setProgressBarProgress(30);
                    }
                }
            }
        }, 500);
    }

    /**
     * 设置进度条的进度
     *
     * @param progress
     */
    private void setProgressBarProgress(int progress) {
        progressBar.setProgress(progress);
        //设置文字的颜色
        setBottomFontStyle(progress);

    }

    /**
     * 设置滑块的位置
     *
     * @param left
     */
    private void setThumbPosition(int left) {
        ivParam.leftMargin = left;
        ivThumb.setLayoutParams(ivParam);
    }

    /**
     * 设置最大进度  默认30
     *
     * @param max
     */
    public void setMax(int max) {
        this.max = max;
        progressBar.setMax(max);
    }

    /**
     * 设置底部选中
     *
     * @param progress 进度
     */
    private void setBottomFontStyle(int progress) {
        int currentValue = progress / 7;
        for (int i = 0; i < mTxtTabs.length; i++) {
            mTxtTabs[i].setSelected(currentValue >= i);
        }
    }

    /**
     * 根据手机的分辨率从 dip 的单位 转成为 px(像素)
     */
    public int dip2px(float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
